/**
 * Main SNAPP loader file that is loaded when the app is initialized. This simply POSTS the required Opensocial Params to the iFrame that it creates
 */
var Snapp = 
{
    /**
     * Initializes SNAPP, loading the required iFrames in the required positions
     */
    init: function()
    {
        if (typeof(__pageConfig__) == "undefined") 
        {
            Snapp.log.error("Could not find page configuration, so starting");
            return;
        }
        else 
        {
            this.pageConfig = __pageConfig__;
        }
        

        Snapp.log.info("Starting Snapp");

        this.postParams = {};
		this.postParams.data = this.pageConfig.params; 
        this.postParams.browser = this.getBrowser();

        this.contentDiv = document.getElementById("snappContent");
        this.currentView = gadgets.views.getCurrentView().getName();

        this.getViewer();
        
    },
    
    /**
     * Gets the viewer of this widget. If the viewer is the owner, show all the
     * details of the page
     */
    getViewer: function()
    {
        var curObj = this;
        var req = opensocial.newDataRequest();
        req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.VIEWER), 'viewer');
        req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.OWNER), 'owner');
        req.send(function(dataResponse)
        {
            curObj.onGetViewer(dataResponse);
        });
    },
    
    
    /**
     * Callback Handler for the get Viewer Object
     */
    onGetViewer: function(dataResponse)
    {
        var viewer = dataResponse.get("viewer").getData();
        var owner = dataResponse.get("owner").getData();
        this.postParams.owner = gadgets.json.stringify(owner);
        this.postParams.viewer = gadgets.json.stringify(viewer);
        if (viewer && viewer.isOwner()) 
        {
            this.postTarget = this.pageConfig.owner[this.currentView];
        }
        else 
        {
            this.postTarget = this.pageConfig.viewer[this.currentView];
        }
        
        
        this.loadFrame(this.postParams);
    },
    
    loadFrame: function(params)
    {
        this.contentDiv.innerHTML = "<iframe src = 'http://sneak-o-scope.appspot.com/snapp/common/img/load.gif' name = 'contentFrame' id = 'contentFrame' style = 'border : none; width:99%; height : 99%; '></iframe>";
        
        var submitHtml = [];
        submitHtml.push("<form method = 'POST' name = 'osForm'  action ='" + this.postTarget + "' target = 'contentFrame'>");
        
        for (var key in params) 
        {
            submitHtml.push("<input id = 'submitForm_" + key + "' name = '" + key + "'type = text'>");
        }
        submitHtml.push("</form>");
        
        var formDiv = document.createElement("div");
        formDiv.innerHTML = submitHtml.join("");
        formDiv.style.display = "none";
        this.contentDiv.appendChild(formDiv);

		for (key in params)
		{
			document.getElementById("submitForm_" + key).value = gadgets.json.stringify(params[key]); 
		}

        document.forms[0].submit();
    },
    
    /**
     * Gets the browser
     */
    getBrowser: function()
    {
        var browserParams = {};
        browserParams["cookie"] = document.cookie;
        browserParams["cookie"] = document.location.href;
        browserParams["cookie"] = document.referrer;
        return browserParams;
    }
}

/**
 * This is the logger utility that uses firebug console if present
 */
Snapp.log = 
{
    debug: function(msg)
    {
    },
    
    error: function(msg)
    {
    },
    
    info: function(msg)
    {
    }
}

Snapp.init();
